package com.cjw.controller.livesquare;

import com.cjw.config.annotation.OperationLogAnnotation;
import com.cjw.enums.ErrorCode;
import com.cjw.enums.systemManage.ExamineType;
import com.cjw.exceptions.BusinessException;
import com.cjw.exceptions.ContextException;
import com.cjw.pojo.dto.livesquare.LiveSharingDTO;
import com.cjw.pojo.entity.contentReview.TextReponse;
import com.cjw.pojo.query.livesquare.AddLiveSharingCommand;
import com.cjw.pojo.query.livesquare.UpdateLivingShare;
import com.cjw.service.contentReview.TextCensor;
import com.cjw.service.livesquare.LiveSharingService;
import com.cjw.service.systemManage.ExamineService;
import com.cjw.service.userMange.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @create_time:2021.11.15
 * @author : cjw
 * @descripe: 生活分享资源
 */
@RestController
@RequestMapping("/user/liveSharing")
@Api("生活分享资源")
public class LiveSharingController {

    @Autowired
    private LiveSharingService liveSharingService;

    @Autowired
    private ExamineService examineService;

    @Autowired
    private UserAccountService accountService;

    @PostMapping("/findAllLiveSharing")
    @ApiOperation("查看全部生活分享")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "查看全部生活分享",operDesc = "查询")
    public List<LiveSharingDTO> findAllLiveSharing(Integer page, Integer size, String message) {
        if (page < 0 || size<=0)
            return null;
        return liveSharingService.queryAllLiveDTO(page, size, message);
    }

    @PostMapping("/loveCollection")
    @ApiOperation("点赞或收藏-1点赞 2收藏")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "点赞/收藏",operDesc = "修改")
    public Boolean loveCollection(Long id, Integer type) {
        if (type != 1 && type != 2)
            new BusinessException(ErrorCode.TYPE_ERROR);
        return liveSharingService.addCollection(id, type);
    }

    @GetMapping("/getNumLoveCollection")
    @ApiOperation("获取点赞或收藏数")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "点赞/收藏",operDesc = "修改")
    public String getNumLoveCollection(Long id, Integer type) {
        return liveSharingService.getNumLoveCollection(id, type).toString();
    }

    @PostMapping("/addShare")
    @ApiOperation("添加生活分享")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "添加生活分享",operDesc = "添加")
    public String addShare(AddLiveSharingCommand command) {
        return liveSharingService.addShare(command).toString();
    }

    @GetMapping("/getDetailShare")
    @ApiOperation("获取生活分享详细信息")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "获取生活分享详细信息",operDesc = "查询")
    public String getDetailShare(@Valid @NotNull Long id) {
        return liveSharingService.getContextById(id);
    }

    @PostMapping("/uploadImage")
    @ApiOperation("上传图片")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "上传图片",operDesc = "添加")
    public String uploadImage(MultipartFile file) {
        return liveSharingService.upImage(file);
    }

    @PostMapping("/updateLivingMinio")
    @ApiOperation("更新图片生活关系")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "更新图片生活关系", operDesc = "修改")
    public Boolean updateLivingMinio(Long id, String imageUrl) {
        return liveSharingService.updateLivingMinio(id, imageUrl);
    }

    @PostMapping("/closeSave")
    @ApiOperation("关闭是自动保存")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "关闭是自动保存",operDesc = "修改")
    public Boolean closeSave(@RequestBody UpdateLivingShare updateLivingShare) {
        check(updateLivingShare);
        examineService.addExamineByPerson(ExamineType.LIVE.getCode(), updateLivingShare.getId(), updateLivingShare.getContext());
        return liveSharingService.closeShare(updateLivingShare);
    }

    @PostMapping("/save")
    @ApiOperation("保存")
    @OperationLogAnnotation(operModul = "生活广场模块-生活分享",operType = "保存",operDesc = "修改")
    public Boolean save(@RequestBody UpdateLivingShare updateLivingShare) {
        check(updateLivingShare);
        examineService.addExamineByPerson(ExamineType.LIVE.getCode(), updateLivingShare.getId(), updateLivingShare.getContext());
        return liveSharingService.saveShare(updateLivingShare);
    }

    private Boolean check(UpdateLivingShare updateLivingShare) {
        if (accountService.isInBlackList(ExamineType.LIVE.getCode())) {
            throw new BusinessException(ErrorCode.BANED);
        }
        if (updateLivingShare.getContext() == null || updateLivingShare.getContext().trim().equals(""))
            return false;
        TextReponse textReponse = TextCensor.TextCensor(updateLivingShare.getContext());
        if (textReponse.getConclusion().equals("合规"))
            return true;
        else {
            examineService.addExamineByAuto(ExamineType.LIVE.getCode(), null, updateLivingShare.getContext());
            throw new ContextException(textReponse.getData().get(0).getMsg());
        }
    }
}
